package hlhj.fhp.checkpoint.atys

import android.content.Intent
import com.bumptech.glide.Glide
import com.example.mymvp.log
import com.huantansheng.easyphotos.EasyPhotos
import hlhj.fhp.checkpoint.BaseAty
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.customView.MyGlideEngin
import hlhj.fhp.checkpoint.iview.IdentityView
import hlhj.fhp.checkpoint.iview.UploadView
import hlhj.fhp.checkpoint.javaBean.MyBaseBean
import hlhj.fhp.checkpoint.presenter.IdentityPresenter
import hlhj.fhp.checkpoint.presenter.UpLoadPresenter
import hlhj.fhp.checkpoint.utils.LoadingUtls
import hlhj.fhp.checkpoint.utils.ToastUtils
import kotlinx.android.synthetic.main.aty_identity.*
import kotlinx.android.synthetic.main.lo_pub_head.*
import java.io.File

/**
 * Created by Never Fear   on 2018\11\13 0013.
Never More....
 */
class IdentityAty :BaseAty<IdentityView,IdentityPresenter>(), IdentityView, UploadView {
    private val TAKEPHOTO1=1_1
    private val TAKEPHOTO2=1_2
    private val TAKEPHOTO3=1_3
    private var f1:File?=null
    private var f2:File?=null
    private var f3:File?=null
    private var path1:String=""
    private var path2:String=""
    private var path3:String=""
    private var a=0
    private var filePresenter:UpLoadPresenter?=null

    override fun commitResp(myBaseBean: MyBaseBean) {
        ToastUtils.toast("提交成功,等待审核",this)
        setResult(-1)
        finish()
    }

    override fun uploadResult(myBaseBean: MyBaseBean, tag: Int) {
        a++
        log("fhp","传了一次了${a}")
        when(tag){
            1->{
                path1=myBaseBean.info.toString()
            }
            2->{
                path2=myBaseBean.info.toString()
            }
            3->{
                path3=myBaseBean.info.toString()
            }
        }
        if (a==3){
            LoadingUtls.diss()
            a=0
            presenter?.commit(this,"张三",path1,path2,path3)
        }
    }

    override fun initView() {
        tvTittle.text="实名认证"
    }

    override fun logicStart() {
    }

    override fun setListener() {
        img1.setOnClickListener {

            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.checkpoint.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO1)
        }
        img2.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.checkpoint.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO2)
        }
        img3.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.checkpoint.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO3)
        }

        btCommit.setOnClickListener {
            filePresenter?.upload(this,f1!!,1)
            filePresenter?.upload(this,f2!!,2)
            filePresenter?.upload(this,f3!!,3)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
        when(requestCode){
            TAKEPHOTO1->{
                Glide.with(this).load(pathList[0]).into(ig1)
                f1=File(pathList[0])
            }
            TAKEPHOTO2->{
                Glide.with(this).load(pathList[0]).into(ig2)
                f2=File(pathList[0])

            }
            TAKEPHOTO3->{
                Glide.with(this).load(pathList[0]).into(ig3)
                f3=File(pathList[0])
            }
        }
    }
    override fun bindView(): IdentityView {
        return this
    }

    override fun createPresenter(): IdentityPresenter {
        filePresenter= UpLoadPresenter()
        filePresenter?.createModule()
        filePresenter?.bindView(this@IdentityAty)
        return IdentityPresenter()
    }

    override val contentId: Int= R.layout.aty_identity
}